
package customer;

import operator.Operator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import callCenter.CallCenter;




/**
 * @author Nugumanova V
 *
 */
public class Customer extends Thread {
	private static final Logger LOGGER = LoggerFactory.getLogger(Customer.class);
	private int id;

	public Customer(int id) {
		this.id = id;
		 
	}
	
	public void run() {			
		LOGGER.info("Customer #" + id + " waits for a response ");
		appealToOperator();
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			LOGGER.error("error");
		}
	}
	
	private void appealToOperator() {
		try {			
			Operator operator = CallCenter.getInstance().getOperatorQueue().take();
			operator.setNumberOfCalls(operator.getNumberOfCalls() + 1);
			CallCenter.getInstance().getOperatorQueue().put(operator);
			LOGGER.info(operator.toString() + "has answered already");			
		} catch (InterruptedException e) {
			LOGGER.error("Unable to contact  operator", e);
		}
		// TODO delete customer
	}

	public long getId() {
		return id;
	}

	@Override
	public String toString() {
		return "Customer [id=" + id + "]";
	}




}
